﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TracedReality.RayTracing;
using QiHe.Yaml.Grammar;

namespace TracedReality.Primitive
{
    /// <summary>
    /// A PointLight that does not cast shadows.
    /// Useful for debugging.
    /// </summary>
    public class NoShadowLight : PointLight
    {
        /// <summary>
        /// Create a NoShadowLight at the given position with the given color.
        /// </summary>
        /// <param name="p">Postion of this NoShadowLight.</param>
        /// <param name="c">Color of this NoShadowLight.</param>
        public NoShadowLight(Vector p, Vector c) : base(p, c) { }

        /// <summary>
        /// Determine how much light from this NoShadowLight is received at the given Hit.
        /// </summary>
        /// <param name="ray">The Ray that is being traced.</param>
        /// <param name="hit">The Hit containing the intersection info.</param>
        /// <param name="r">Random for sampling.</param>
        /// <returns>The contribution of this NoShadowLight.</returns>
        public override Vector getContribution(Ray ray, Hit hit, Random r)
        {
            Vector contrib = new Vector();
            Vector point = ray.pointAtParameter(hit.t);
            Vector lightDir = position - point;
            lightDir.normalize();

            return hit.material.shade(ray, hit, lightDir, color);
        }

        public static new NoShadowLight Parse(MappingEntry entry)
        {
            if (entry == null) { throw new ArgumentNullException("entry"); }

            Mapping map = (Mapping)entry.Value;
            MappingEntry found = map.Find("color");
            if (found == null) { throw new ArgumentException("No color entry"); }
            Vector color = Vector.FromSequence((Sequence)found.Value);
            found = map.Find("position");
            if (found == null) { throw new ArgumentException("No position entry"); }
            Vector position = Vector.FromSequence((Sequence)found.Value);

            return new NoShadowLight(position, color);
        }

        /// <summary>
        /// Gets a string representation of this NoShadowLight.
        /// </summary>
        /// <returns>A string representing this NoShadowLight.</returns>
        public override string ToString()
        {
            return "NoShadowLight: { position: " + position + ", color: " + color + " }";
        }
    }
}
